package com.ruoyi.gis.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gis.domain.*;
import com.ruoyi.gis.service.DeviceDataService;
import com.ruoyi.gis.service.IYcGroupLayerRptService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/gis/data/ycRpt")
public class YcGroupLayerRptController extends BaseController {

    @Autowired
    private IYcGroupLayerRptService ycRptService;

    /**
     * 查询长势氮磷钾统计数据
     * 根据图层code与地区code ()
     * (按区域氮磷钾统计数据)
     * @param groupCode
     * @return
     */
    @GetMapping("/ycLayerRptDLJList/{groupCode}")
    public AjaxResult ycLayerRptDLJList(@PathVariable("groupCode") String groupCode)
    {
        List<YcGroupLayerRpt> list = ycRptService.selectDLJListByGroupName(groupCode);
        if (list == null || list.isEmpty()) {
            return AjaxResult.success("无数据");
        }
        Map<String,Object> retMap = new HashMap<>();
        List<JSONObject> retList = new ArrayList<>();
        Map<String,List<YcGroupLayerRpt>> groupTypeMap = list.stream()
                .collect(Collectors.groupingBy(YcGroupLayerRpt::getGroupType));
        int index = 1;
        List<String> keys = new ArrayList<>(groupTypeMap.keySet());
        for (String key : keys) {
            List<YcGroupLayerRpt> value = groupTypeMap.get(key);
            JSONObject jsonObject = new JSONObject();
            System.out.println("Group: " + key);
            jsonObject.put("groupType",key);
            jsonObject.put("xAxisData",value.stream().map(YcGroupLayerRpt::getxName).collect(Collectors.toList()));

            List<JSONObject> objectList = new ArrayList<>();
            JSONObject data1 = new JSONObject();
            data1.put("code","ypQdhlValue");
            data1.put("name","叶片全氮含量（%）");
            data1.put("value",value.stream().map(YcGroupLayerRpt::getYpQdhlValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","ypQlhlValue");
            data1.put("name","叶片全磷含量（%）");
            data1.put("value",value.stream().map(YcGroupLayerRpt::getYpQlhlValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","ypSxjhlValue");
            data1.put("name","叶片速效钾含量(%)");
            data1.put("value",value.stream().map(YcGroupLayerRpt::getYpSxjhlValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","jgQdhlValue");
            data1.put("name","茎秆全氮含量（%）");
            data1.put("value",value.stream().map(YcGroupLayerRpt::getJgQdhlValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","jgQlhlValue");
            data1.put("name","茎秆全磷含量（%）");
            data1.put("value",value.stream().map(YcGroupLayerRpt::getJgQlhlValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","jgSxjhlValue");
            data1.put("name","茎秆速效钾含量(%)");
            data1.put("value",value.stream().map(YcGroupLayerRpt::getJgSxjhlValue).collect(Collectors.toList()));
            objectList.add(data1);

            jsonObject.put("data",objectList);
            retList.add(jsonObject);
            index ++;
        };


        return AjaxResult.success(retList);
    }

    /**
     * 查询物候统计数据
     * 按图层code与区域code查询
     * @param groupCode
     * @return
     */
    @GetMapping("/ycLayerRptWHList/{groupCode}")
    public AjaxResult ycLayerRptWHList(@PathVariable("groupCode") String groupCode){
        List<YcGroupLayerRpt> list = ycRptService.selectWHListByGroupName(groupCode);
        Collections.sort(list,Comparator.comparing(YcGroupLayerRpt::getxName));
        if (list == null || list.isEmpty()) {
            return AjaxResult.success("无数据");
        }

        List<JSONObject> retList = new ArrayList<>();

        Map<String,List<YcGroupLayerRpt>> groupTypeMap = list.stream()
                .collect(Collectors.groupingBy(YcGroupLayerRpt::getGroupType));
        int index = 1;
        List<String> keys = new ArrayList<>(groupTypeMap.keySet());
        String oneKey = keys.get(0);
        for (String key : keys) {
            List<YcGroupLayerRpt> value = groupTypeMap.get(key);
            JSONObject jsonObject = new JSONObject();
            System.out.println("Group: " + key);
            jsonObject.put("groupType",key);
            jsonObject.put("xAxisData",value.stream().map(YcGroupLayerRpt::getxName).collect(Collectors.toList()));

            List<JSONObject> objectList = new ArrayList<>();
            JSONObject data1 = new JSONObject();
            data1.put("code","yValue");
            data1.put("name","株数");
            data1.put("value",value.stream().map(YcGroupLayerRpt::getyValue).collect(Collectors.toList()));
            objectList.add(data1);

            jsonObject.put("data",objectList);
            retList.add(jsonObject);
            index ++;
        };
        //取第一个数据作为总体x轴数据
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("xAxisData",groupTypeMap.get(oneKey).stream().map(YcGroupLayerRpt::getxName).collect(Collectors.toList()));
        retMap.put("dataList",retList);

        return AjaxResult.success(retMap);
    }


    /**
     * 统计面积数据
     * @param mjQuery
     * @return
     */
    @PostMapping("/ycLayerRptMjList")
    public AjaxResult ycLayerRptMjList(@RequestBody YcMjData mjQuery){
        Map<String,Object> retMap = new HashMap<>();
        //retMap.put("tjData",null);//统计数据
        //retMap.put("datailData",null);//详细数据
        if(mjQuery==null){
            return AjaxResult.success("传入参数错误");
        }
        if(mjQuery.getGroupPCodes()!=null && mjQuery.getGroupPCodes().size()>0){
            //按父级查询面积，并放入retMap中
            YcMjData ycMjData = ycRptService.selectMjDataByGroupCode(mjQuery.getGroupPCodes());
            if(ycMjData!=null){
                retMap.put("groupMjData",ycMjData.getSbMjValue());
            }else{
                retMap.put("groupMjData",null);
            }
        }
        if(mjQuery.getGroupCodes()!=null && mjQuery.getGroupCodes().size()>0){
            //按子级查询面积，并放入retMap中
            YcMjData ycMjData = ycRptService.selectMjDataByProductCode(mjQuery.getGroupCodes());
            if(ycMjData!=null){
                retMap.put("productMjData",ycMjData.getSbMjValue());
            }else{
                retMap.put("productMjData",null);
            }

        }

        return AjaxResult.success(retMap);
    }


    /**
     * 根据年份获取全国面积数据
     * @param strNf
     * @return
     */
    @GetMapping("/ycLayerRptQgMjList/{strNf}")
    public AjaxResult ycLayerRptQgMjList(@PathVariable("strNf") String strNf){
        if(StringUtils.isEmpty(strNf)){
            return AjaxResult.success("传入参数错误");
        }
        List<YcProvinceData> list = ycRptService.selectProvinceMjDataByNf(strNf);
        if(list == null || list.size()<=0){
            return AjaxResult.success("无数据");
        }
        List<String> xAxisDataList = new ArrayList<>();
        List<String> valueList = new ArrayList<>();
        for(YcProvinceData ycProvinceData:list){
            xAxisDataList.add(ycProvinceData.getProvince());
            valueList.add(ycProvinceData.getMj());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("xAxisData",xAxisDataList);
        jsonObject.put("value",valueList);

        return AjaxResult.success(jsonObject);
    }


    /**
     * 获取无人机地块统计数据
     * 根据layerCode xName年份
     */
    @PostMapping("/ycLayerRptUavList")
    public AjaxResult ycLayerRptUavList(@RequestBody YcBxData queryYcBxData){
        if(queryYcBxData==null || StringUtils.isEmpty(queryYcBxData.getLayerCode()) || StringUtils.isEmpty(queryYcBxData.getxName())){
            return AjaxResult.success("传入参数错误");
        }
        List<YcBxData> list = ycRptService.selectDkBxList(queryYcBxData);
        if (list == null || list.isEmpty()) {
            return AjaxResult.success("无数据");
        }
        Map<String,Object> retMap = new HashMap<>();
        List<JSONObject> retList = new ArrayList<>();
        Map<String,List<YcBxData>> groupTypeMap = list.stream().collect(Collectors.groupingBy(YcBxData::getxName));
        int index = 1;
        List<String> keys = new ArrayList<>(groupTypeMap.keySet());
        for (String key : keys) {
            List<YcBxData> value = groupTypeMap.get(key);
            JSONObject jsonObject = new JSONObject();
            System.out.println("Group: " + key);
            jsonObject.put("groupType",key);
            jsonObject.put("xAxisData",value.stream().map(YcBxData::getGroupType).collect(Collectors.toList()));

            List<JSONObject> objectList = new ArrayList<>();
            JSONObject data1 = new JSONObject();
            data1.put("code","zgValue");
            data1.put("name","株高/cm");
            data1.put("value",value.stream().map(YcBxData::getZgValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","gjcValue");
            data1.put("name","根茎粗/mm");
            data1.put("value",value.stream().map(YcBxData::getGjcValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","ymjValue");
            data1.put("name","叶面积/cm2");
            data1.put("value",value.stream().map(YcBxData::getYmjValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","yxzValue");
            data1.put("name","叶鲜重/g");
            data1.put("value",value.stream().map(YcBxData::getYxzValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","jxzValue");
            data1.put("name","茎鲜重/g");
            data1.put("value",value.stream().map(YcBxData::getJxzValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","gxzValue");
            data1.put("name","根鲜重/g");
            data1.put("value",value.stream().map(YcBxData::getGxzValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","gxzValue");
            data1.put("name","根鲜重/g");
            data1.put("value",value.stream().map(YcBxData::getGxzValue).collect(Collectors.toList()));
            objectList.add(data1);


            data1 = new JSONObject();
            data1.put("code","ygzValue");
            data1.put("name","叶干重/g");
            data1.put("value",value.stream().map(YcBxData::getYgzValue).collect(Collectors.toList()));
            objectList.add(data1);


            data1 = new JSONObject();
            data1.put("code","jgzValue");
            data1.put("name","茎干重/g");
            data1.put("value",value.stream().map(YcBxData::getJgzValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","ggzValue");
            data1.put("name","根干重/g");
            data1.put("value",value.stream().map(YcBxData::getGgzValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","dsbxzValue");
            data1.put("name","地上部鲜重/g");
            data1.put("value",value.stream().map(YcBxData::getDsbxzValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","dsbgzValue");
            data1.put("name","地上部干重/g");
            data1.put("value",value.stream().map(YcBxData::getDsbgzValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","mdValue");
            data1.put("name","密度（株/m2）");
            data1.put("value",value.stream().map(YcBxData::getMdValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","ymjzsValue");
            data1.put("name","叶面积指数");
            data1.put("value",value.stream().map(YcBxData::getYmjzsValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","dshbxzzsValue");
            data1.put("name","地上部鲜重指数");
            data1.put("value",value.stream().map(YcBxData::getDsbxzzsValue).collect(Collectors.toList()));
            objectList.add(data1);

            data1 = new JSONObject();
            data1.put("code","dsbgzzsValue");
            data1.put("name","地上部干重");
            data1.put("value",value.stream().map(YcBxData::getDsbgzzsValue).collect(Collectors.toList()));
            objectList.add(data1);


            jsonObject.put("data",objectList);
            retList.add(jsonObject);
            index ++;
        };


        return AjaxResult.success(retList);

    }


    /**
     * 根据年份 统计全国油菜数据
     * @param strNf
     * @return
     */
    @GetMapping("/ycLayerRptQgTj/{strNf}")
    public AjaxResult ycLayerRptQgTj(@PathVariable("strNf") String strNf){
        if(StringUtils.isEmpty(strNf)){
            return AjaxResult.success("传入参数错误");
        }
        //按年份 查询出春秋油菜面积数
        List<YcQgbzMjData> list = ycRptService.selectYcRptQgMjByNf(strNf);
        if(list == null || list.size()<=0){
            return AjaxResult.success("无数据");
        }
        Map<String,Object> retMap = new HashMap<>();
        //按播种类型 统计全国总数
        Map<String,Double> bozhongTypeMap = list.stream().collect(Collectors.groupingBy(YcQgbzMjData::getBozhongTypeCode,Collectors.summingDouble(YcQgbzMjData::getMjValue)));
        retMap.putAll(bozhongTypeMap);
        //retMap.put("cycMj",0.00);
        //retMap.put("dycMj",0.00);

        //按区域统计全国总数(饼图)
        Map<String,Double> qyMap = list.stream().collect(Collectors.groupingBy(YcQgbzMjData::getQyName,Collectors.summingDouble(YcQgbzMjData::getMjValue)));
        retMap.put("qyList",qyMap);

        //按省份统计全国总数(柱状图)
        Map<String,Double> sfMap = list.stream().collect(Collectors.groupingBy(YcQgbzMjData::getSfName,Collectors.summingDouble(YcQgbzMjData::getMjValue)));
        retMap.put("sfList",sfMap);



        return AjaxResult.success(retMap);
    }

}
